Ext.define('Sencha.controller.DictionarySourceController', {
    extend: 'Ext.app.Controller',

    config: {
        refs: {
            main: 'dictionarysourcenavigationview',
            newDictionaryButton: 'button[cls=addDictionaryButton]',
            editDictionaryButton: 'button[cls=editDictionaryButton]',
            dictionarySearchField: 'searchfield[cls=dictionarySearchField]',
            dictionarySourceListView: 'dictionarysourcelistview',
            engDictionaryButtonOn: 'button[cls=engDictionaryButtonOn]',
            engDictionaryButtonOff: 'button[cls=engDictionaryButtonOff]'
        },
        control: {
            'dictionarysourcelistview': {
                disclose: 'showDetail',
                select: 'playAudio',
            },
            'button[cls=addDictionaryButton]':{
            	tap:'showForm'
            },
           
            'dictionarysourcenavigationview':{
            	back:'backNavigationView'
            },
           
           'searchfield[cls=dictionarySearchField]':{        
           		keyup:'filterDictionarySearch'	
           },
           
           'button[cls=engDictionaryButtonOn]' :{
           		tap: 'toggleOffEngDictionaryButton'
           }
           
        }
    },

    //show detail view for specific dictionary source word
    showDetail: function(list, record) {
        var store = Ext.data.StoreManager.get("DictionaryTargets"); 
        //store.clearFilter();
        //store.filter('id', 1);
        
        /*var names = store.getData();   
           alert(store.getCount());
        
           var first = store.first();
           var names = first.getFields();
           
           alert(names.length);
           
           for(var x in names)
           {
           alert(names[x]);
           
           }*/
         
      
               
		this.getMain().push({
            xtype: 'dictionarytargetdetailview',
            
            //data: store.getData()
            data: record.getData()
        });
    },
    
    //showing add form for dictionary
   	showForm : function(){

           
        var bar = this.getMain().getNavigationBar();
        this.getNewDictionaryButton().hide();
               
        bar.add({
            xtype   : 'button',
            text    : 'EDIT',
            align 	: 'right',
            cls		: 'editDictionaryButton'
        });
           
    	this.getMain().push({
    		title: 'Second',
    		xtype: 'dictionaryformview'			                    
        });
           
        
   	},
    
    
    //play audio when list item is selected       
    playAudio: function() {
        alert("playAudio");
    },
    
    //fired when back button is pressed on dictionary 
    backNavigationView: function(){
        this.getNewDictionaryButton().show();
    	this.getEditDictionaryButton().hide();
    },
    
    
    //fired when value has changed on dictionarySearchField
    filterDictionarySearch: function() {
    	var sto = this.getDictionarySourceListView().getStore();
    	sto.clearFilter();
    	sto.filter('sourceWord', this.getDictionarySearchField().getValue());
    	
    },        
    
    toggleOffEngDictionaryButton: function() {
    	
           
        if (typeof this.getIwaDictionaryButtonOff() === "undefined"){ 
           
        }else{
        	this.getEngDictionaryButtonOn().setCls('engDictionaryButtonOff');
        }
        
    }

});
